package com.dongdong.bookstore.module.ums.controller;


import com.dongdong.bookstore.common.api.CommonPage;
import com.dongdong.bookstore.common.exception.Asserts;
import com.dongdong.bookstore.module.ums.model.Menu;
import com.dongdong.bookstore.module.ums.service.MenuService;
import com.dongdong.bookstore.module.ums.service.UmsCacheService;
import com.dongdong.bookstore.module.ums.viewmodel.MenuNode;
import com.dongdong.bookstore.module.ums.viewmodel.MenuParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.parameters.P;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import java.util.List;

/**
 * <p>
 * 菜单表  前端控制器
 * </p>
 *
 * @author chiangkai
 * @since 2021-10-20
 */
@RestController
@Api(tags = "后台菜单管理")
@RequestMapping("/ums/menu")
public class MenuController {

    @Autowired
    private MenuService menuService;

    @Autowired
    private UmsCacheService umsCacheService;
    @ApiOperation("添加后台菜单")
    @PostMapping("/create")
    public String create(@RequestBody MenuParam menu) {
        menuService.create(menu);
        return "创建菜单成功";
    }

    @PostMapping("/update/{id}")
    @ApiOperation("修改后台菜单")
    public String update(@PathVariable Long id, @RequestBody MenuParam menuParam) {
        menuService.update(id, menuParam);
        umsCacheService.clearMenuListAll();
        return "更新成功";
    }

    @ApiOperation("根据ID获取菜单")
    @GetMapping("/{id}")
    public Menu getMenuById(@PathVariable("id") Long id) {
        return menuService.getById(id);
    }

    @ApiOperation("根据ID删除菜单")
    @GetMapping("/delete/{id}")
    public String delete(@PathVariable Long id) {
        Asserts.isFalse(menuService.removeById(id), "删除失败");
        umsCacheService.clearMenuListAll();
        return "删除成功";
    }

    @ApiOperation("获取菜单列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "parentId", value = "菜单父ID", required = true, paramType = "path"),
            @ApiImplicitParam(name = "pageSize", value = "每页数量", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "pageNum", value = "当前页码", dataType = "Integer", paramType = "query")
    })
    @GetMapping("/list/{parentId}")
    public CommonPage<Menu> list(@PathVariable Long parentId,
                                 @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                 @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        return CommonPage.restPage(menuService.list(parentId, pageSize, pageNum));
    }

    @ApiOperation("获取菜单树形结构")
    @GetMapping("/treeList")
    public List<MenuNode> treeList(){
        return menuService.treeList();
    }

    @ApiOperation("修改菜单显示状态")
    @RequestMapping(value = "/updateHidden/{id}", method = RequestMethod.POST)
    @ResponseBody
    public String updateHidden(@PathVariable Long id, @RequestParam("hidden") @Validated @Max(value = 1,message = "更新菜单显示状态参数错误") @Min(value = 0,message = "更新显示状态参数错误") Integer hidden) {
        menuService.updateHidden(id, hidden);
        return "更新成功";
    }
}
